<?php

//列表页控制器
class ListController extends Controller{
	private $model;
	
	public function __init(){
		$this->model=K('List');
	}
	
	public function index(){
		//显示列表商品
		$cid=Q('get.cid');
		
		//获得顶级分类cid
		$pid=K('Category')->where("cid={$cid}")->find();
		$pids=$pid['pid'];
		
		
		//获得当前分类的子集cid
//		$cids = $this->_getSon($pids);
		
		$gids = K('Goods')->where("cid={$cid}")->getField('gid',true);
		
		if($gids){
			//通过商品gid得到属性
			$attr = K('Goodsattr')->where("gid in(" . implode(',', $gids) . ")")->group('gtvalue')->all();
			//以下两个临时数组为重组列表页需要的属性
			$tempAttr = array();
			foreach ($attr as $v) {
				$tempAttr[$v['taid']][] = $v;
			}
			
			$tempFinalAttr = array();
			//组合有名称name的属性
			foreach ($tempAttr as $taid => $value) {
				$tempFinalAttr[] = array(
					'name' => K('Attr')->where("taid=" . $taid)->getField('taname'),
					'value' => $value
				);
			}
           
			$this->assign('tempFinalAttr',$tempFinalAttr);
			//List/index/cid/1/param/0_0_0_0_0
			$num = count($tempFinalAttr);
			$param = isset($_GET['param']) ? explode('_', $_GET['param']) : array_fill(0, $num, 0);
			$this->assign('param',$param);
			//进行筛选
			foreach ($param as $v) {
				if($v){
					$filterGids[] = M()->join('__goods_attr__ g1 JOIN __goods_attr__ g2 ON g1.gtvalue=g2.gtvalue')->where("g1.gtid={$v}")->getField('g2.gid',true);
				}
			}
			//如果有筛选的gid
			if(isset($filterGids)){
				//取交集
				$finalGids = $filterGids[0];
				foreach ($filterGids as $v) {
					$finalGids = array_intersect($finalGids, $v);
				}
				//属性获得交集gid之后，再和分类下面的商品gid再取交集
				$finalGids = array_intersect($finalGids,$gids);
				
				
			}else{
				//全部不限的时候，就是当前分类下面的所有商品gid
				$finalGids = $gids;
				
				
			}
		}else{
			//如果分类下面没有商品,把gid赋值为空数组
			$finalGids = array();
			
		}
		if($finalGids){
			$goodsdata= K('Goods')->where("gid in(" . implode(',', $finalGids) . ")")->all();
			$this->assign('goodsdata',$goodsdata);
		}
		
		
		
		//显示顶部分类
		$da=K('Category')->where("cid={$cid}")->find();
        $da['temp']=K('Category')->where("pid={$da['pid']} AND cid!={$cid}")->all();
        $da['father']=K('Category')->where("cid={$da['pid']}")->field("cname")->find();
		$this->assign('da',$da);
		
		//显示筛选部分
		$tid=K('Category')->where("cid={$cid}")->field('tid')->find();
		$tid=$tid['tid'];
		$attr=K('Attr')->where("tid={$tid}")->all();
		foreach ($attr as $k => $v) {
			$attr[$k]['tavalue']=explode(',', $v['tavalue']);
		}
		
		$this->assign('attr',$attr);
		
		$this->display();
	}
	
//	private function _getSon($cid){
//		$data = K('Category')->all();
//		$cids = $this->_getSonCid($data,$cid);
//		$cids[] = $cid;
//		return $cids;
//	}
//	
//	private function _getSonCid($data,$cid){
//		$temp = array();
//		foreach ($data as $v) {
//			if($v['pid'] == $cid){
//				$temp[] = $v['cid'];
//				$temp = array_merge($temp,$this->_getSonCid($data, $v['cid']));
//			}
//		}
//		return $temp;
//	}
	
}

?>